package com.code.generator.cn.util.fb;

import com.code.generator.cn.entity.CoderField;
import com.code.generator.cn.entity.CoderTable;
import com.code.generator.cn.util.MyFileUtil;
import com.code.generator.cn.util.PathUtil;
import com.code.generator.cn.util.base.CommonUtil;
import com.code.generator.cn.util.base.ServiceUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FbServiceUtil extends ServiceUtil {

    public static void createService(CoderTable coderTable, List<CoderField> coderFields, Configuration configuration, String projPath) throws IOException, TemplateException {
        //生成接口
        createIService(coderTable, coderFields, projPath, configuration);
        //生成实现类
        createServiceImpl(coderTable, coderFields, projPath, configuration);
    }

    private static void createServiceImpl(CoderTable coderTable, List<CoderField> coderFields, String projPath, Configuration configuration) throws IOException, TemplateException {
        Writer file = null;
        String className = coderTable.getModifyName();
        Template temp = configuration.getTemplate("/fb/IService.ftl");

        //获取包名
        String packageName  = PathUtil.getCreateBasePath(coderTable);



        String groupPath = packageName.replace(".", "/");
        //生成entity的路径
        String mapperPath = projPath + "/src/main/java/" + groupPath + "/service/";
        //设置生成的路径
        MyFileUtil.createFileDir(mapperPath);
        //设置生成的属性
        Map<String, Object> createMap = new HashMap<>();
        CommonUtil.createFbBaseInfo(coderTable, createMap);
        CommonUtil.createPrimary(coderFields, createMap);
        //设置生成的文件名
        file = new FileWriter(mapperPath + "I" + className + "Service.java");
        temp.process(createMap, file);
        file.close();


    }


    private static void createIService(CoderTable coderTable, List<CoderField> coderFields, String projPath, Configuration configuration) throws IOException, TemplateException {
        Writer file = null;
        String className = coderTable.getModifyName();
        Template temp = configuration.getTemplate("/fb/serviceImpl.ftl");

        //获取包名
        String packageName  = PathUtil.getCreateBasePath(coderTable);



        String groupPath = packageName.replace(".", "/");
        //生成entity的路径
        String mapperPath = projPath + "/src/main/java/" + groupPath + "/service/impl/";
        //设置生成的路径
        MyFileUtil.createFileDir(mapperPath);
        //设置生成的属性
        Map<String, Object> createMap = new HashMap<>();
        CommonUtil.createFbBaseInfo(coderTable, createMap);
        CommonUtil.createPrimary(coderFields, createMap);
        //设置生成的文件名
        file = new FileWriter(mapperPath + className + "ServiceImpl.java");
        temp.process(createMap, file);
        file.close();


    }
}
